package com.petcommunity.dao;

import com.petcommunity.model.VaccineRecord;
import com.petcommunity.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class VaccineRecordDAO {

    public void addRecord(VaccineRecord record) {
        String sql = "INSERT INTO vaccine_records (pet_id, vaccine_name, vaccination_date, next_due_date) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, record.getPetId());
            stmt.setString(2, record.getVaccineName());
            stmt.setDate(3, new java.sql.Date(record.getVaccinationDate().getTime()));
            stmt.setDate(4, new java.sql.Date(record.getNextDueDate().getTime()));
            stmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<VaccineRecord> getAllRecords() {
        List<VaccineRecord> list = new ArrayList<>();
        String sql = "SELECT * FROM vaccine_records ORDER BY record_id ASC";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                VaccineRecord record = new VaccineRecord();
                record.setRecordId(rs.getInt("record_id"));
                record.setPetId(rs.getInt("pet_id"));
                record.setVaccineName(rs.getString("vaccine_name"));
                record.setVaccinationDate(rs.getDate("vaccination_date"));
                record.setNextDueDate(rs.getDate("next_due_date"));
                list.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public VaccineRecord getVaccineById(int id) {
    VaccineRecord vaccine = null;
    try (Connection conn = DBUtil.getConnection()) {
        String sql = "SELECT * FROM vaccine_records WHERE record_id = ?";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    // 实例化 VaccineRecord 对象
                    vaccine = new VaccineRecord();
                    vaccine.setRecordId(rs.getInt("record_id"));
                    vaccine.setPetId(rs.getInt("pet_id"));
                    vaccine.setVaccineName(rs.getString("vaccine_name"));
                    vaccine.setVaccinationDate(rs.getDate("vaccination_date"));
                    vaccine.setNextDueDate(rs.getDate("next_due_date"));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return vaccine;
}

    public void updateVaccine(VaccineRecord vaccine) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE vaccine_records SET pet_id=?, vaccine_name=?, vaccination_date=?, next_due_date=? WHERE record_id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, vaccine.getPetId());
            ps.setString(2, vaccine.getVaccineName());
            ps.setDate(3, vaccine.getVaccinationDate());
            ps.setDate(4, vaccine.getNextDueDate());
            ps.setInt(5, vaccine.getRecordId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteVaccine(int id) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM vaccine_records WHERE vaccine_id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

